package com.sz.stock.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sz.stock.mapper.EntrustMapper;
import com.sz.stock.model.Entrust;
import com.sz.stock.model.EntrustExample;
import com.sz.stock.model.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class EntrustServiceImp implements IEntrustService {

    @Autowired
    private EntrustMapper entrustMapper;

    /**
     * 查询 所有的委托信息
     */
    @Override
    public List<Entrust> showEntrust(int userid) {
        EntrustExample example = new EntrustExample();
        EntrustExample.Criteria criteria = example.createCriteria();
        criteria.andStockEntrustUseridEqualTo(userid);
        List<Entrust> entrusts = entrustMapper.selectByExample(example);
        return entrusts;
    }

//    /**
//     * 添加 委托信息
//     */
//    @Override
//    public int addEntrust(Entrust entrust) {
//        return entrustMapper.insertSelective(entrust);
//    }

    /**
     * 添加 委托信息
     */
    @Override
    public int addEntrust1(Entrust entrust) {
        return entrustMapper.insert(entrust);
    }

    /**
     * 查询 未成交,和部分成交的 委托信息(状态 :未成交,部成)
     */
    @Override
    public List<Entrust> keywordEntrust(int userid) {
        List<String> list = new ArrayList<>();
        list.add("未成交");
        list.add("部成");
        EntrustExample example = new EntrustExample();
        EntrustExample.Criteria criteria = example.createCriteria();
        criteria.andStockEntrustUseridEqualTo(userid);
        criteria.andStockEntrustStatusIn(list);
        return entrustMapper.selectByExample(example);
    }

    /**
     * 查询 委托信息(状态 :未成交,部成)  带分页
     */
    @Override
    public PageBean<Entrust> keywordEntrust2(int userid, int pageIndex, int pageSize, String stockEntrustCode) {
        List<String> list2 = new ArrayList<>();
        list2.add("未成交");
        list2.add("部成");
        PageHelper.startPage(pageIndex, pageSize);
        EntrustExample example = new EntrustExample();
        EntrustExample.Criteria criteria = example.createCriteria();
        if (stockEntrustCode != null && !stockEntrustCode.equals("")) {
            criteria.andStockEntrustCodeEqualTo(stockEntrustCode);
        }
        criteria.andStockEntrustStatusIn(list2);
        criteria.andStockEntrustUseridEqualTo(userid);
        List<Entrust> list = entrustMapper.selectByExample(example);
        //带模糊查询并分页的数据,如果stockEntrustCode为空,则查询所有
        Page<Entrust> plist = (Page<Entrust>) list;
        PageBean<Entrust> pb = new PageBean<>(
                plist.getTotal(), plist.getResult(),
                plist.getPageSize(), plist.getPages(), plist.getPageNum()
        );
        return pb;
    }

    /**
     * 查询 委托信息(状态 :未成交,部成)  带分页
     */
    @Override
    public PageBean<Entrust> keywordEntrust3(int userid, int pageIndex, int pageSize, Entrust entrust) {
        PageHelper.startPage(pageIndex, pageSize);
        EntrustExample example = new EntrustExample();
        EntrustExample.Criteria criteria = example.createCriteria();
        if (entrust != null && entrust.getStockEntrustCode().length() > 0) {
            criteria.andStockEntrustCodeEqualTo(entrust.getStockEntrustCode());
        }
        if (entrust != null && entrust.getStockEntrustStatus().length() > 0) {
            criteria.andStockEntrustStatusEqualTo(entrust.getStockEntrustStatus());
        }
        criteria.andStockEntrustUseridEqualTo(userid);
        List<Entrust> list = entrustMapper.selectByExample(example);
        //带模糊查询并分页的数据,如果stockEntrustCode为空,则查询所有
        Page<Entrust> plist = (Page<Entrust>) list;
        PageBean<Entrust> pb = new PageBean<>(
                plist.getTotal(), plist.getResult(),
                plist.getPageSize(), plist.getPages(), plist.getPageNum()
        );
        return pb;
    }

////    查买入委托(用户以外的,股票代码一样的,操作是:买入的,状态是: 未成交或部成的,委托价大于或等于的)
//    @Override
//    public List<Entrust> keywordEntrust5(Entrust entrust1) {
//        List<String> list2=new ArrayList<>();
//        list2.add("未成交");
//        list2.add("部成");
//        EntrustExample example=new EntrustExample();
//        EntrustExample.Criteria criteria = example.createCriteria();
//        criteria.andStockEntrustUseridNotEqualTo(entrust1.getStockEntrustUserid());  //用户以外的
//        criteria.andStockEntrustCodeEqualTo(entrust1.getStockEntrustCode());   //股票代码一样的
//        criteria.andStockEntrustOperateEqualTo("买入");     //操作是:买入的
//        criteria.andStockEntrustStatusIn(list2);      //状态是: 未成交或部成的
//        criteria.andStockEntrustMoneyGreaterThanOrEqualTo(entrust1.getStockEntrustMoney());  //委托价大于或等于的
//        return entrustMapper.selectByExample(example);
//    }

    /**
     *   查买入委托信息
     */
    @Override
    public List<Entrust> selectCondition(Integer stockEntrustUserid, String stockEntrustCode, BigDecimal stockEntrustMoney) {
        int c = stockEntrustMoney.intValue();
        return entrustMapper.selectCondition(stockEntrustUserid,stockEntrustCode,c);
    }

    /**
     *   修改委托表信息 (只修改有值的字段)
     */
    @Override
    public int updateStatus(Entrust entrust) {
        return entrustMapper.updateByPrimaryKeySelective(entrust);
    }

    /**
     * 查询一条委托表信息
     */
    @Override
    public Entrust getOneEntrust(Integer stockEntrustId) {
        return entrustMapper.selectByPrimaryKey(stockEntrustId);
    }

    /**
     * 查询 用户状态为  买入 或卖出的 委托表
     */
    @Override
    public List<Entrust> getEntrustByOperate(int userid, String stockEntrustOperate) {
        EntrustExample example = new EntrustExample();
        EntrustExample.Criteria criteria = example.createCriteria();
        criteria.andStockEntrustOperateEqualTo(stockEntrustOperate);
        criteria.andStockEntrustUseridEqualTo(userid);
        return entrustMapper.selectByExample(example);
    }

    /**
     * 查询 卖出 委托信息(判断是否可以成交)
     */
    @Override
    public List<Entrust> selectSellEntrust(Entrust entrust) {
        float i = entrust.getStockEntrustMoney().floatValue();
        return entrustMapper.selectSellEntrust(entrust.getStockEntrustCode(), i);
    }

    /**
     * 通过合同编号查询委托信息(成交用)
     */
    @Override
    public Entrust getEntrustByContract(String contractID) {
        Entrust entrust = new Entrust();
        EntrustExample example = new EntrustExample();
        EntrustExample.Criteria criteria = example.createCriteria();
        criteria.andStockEntrustContractidEqualTo(contractID);
        List<Entrust> entrusts = entrustMapper.selectByExample(example);
        for (Entrust entrust1 : entrusts) {
            entrust=entrust1;
        }
        return entrust;
    }

}
